<?php
class EmailOffers extends Model_Abstract
{
	public function getOfferEmailSwitch(){
		$sql = "SELECT v FROM configs WHERE k='offerEmailSwitch'";
		try {
			$result = $this->_connection->query($sql);
			return $result[0][v];
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	public function getOfferEmailSubject($site_id){
		$k = 'offerEmailSubject' . $site_id;
		$sql = " SELECT v FROM configs WHERE k=? ";
		try {
			$result = $this->_connection->query($sql, array($k));
			return $result[0][v];
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	public function setOfferEmailSubject($subject, $site_id){
		$k = 'offerEmailSubject' . $site_id;
		$sql = " SELECT k FROM configs WHERE k=? ";
		try {
			$result = $this->_connection->query($sql, array($k));
			if(empty($result)){
				$sql = " INSERT INTO configs SET v=?, k=?  ";
			}else{
				$sql = "UPDATE configs SET v=? WHERE k=? ";
			}
			return $this->_connection->execute($sql, array($subject, $k));
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	public function setEmailSwitch($v){
		$sql = "UPDATE configs SET v=? WHERE k='offerEmailSwitch'";
		try {
			return $this->_connection->query($sql, array($v));
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function getAllEmailOffers() {
		$sql = 'select * from email_offers';
		try {
			return $this->_connection->query($sql);
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function getEmailOfferById ($id) {
		$sql = 'select * from email_offers where id=?';
		try {
			$reuslt = $this->_connection->query($sql, array($id));
			if (count($reuslt) > 0) {
				return $reuslt[0];
			}
			return null;
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function searchEmailOffers($siteId, $name, $offerName) {
		$condtion = array();
		$vars = array();
		if (!empty($siteId)) {
			$condition[] = 'site_id=?';
			$vars[] = $siteId;
		}
		if (!empty($name)) {
			$condition[] = 'name like ?';
			$vars[] = "%" . trim($name) . "%";
		}
		if (!empty($offerName)) {
			$condition[] = "offer_name like ?";
			$vars[] = "%" . trim($offerName) . "%";
		}
		$sql = "select * from email_offers ";
		if (count($condition) > 0) {
			$sql .= 'where ' . implode(' and ', $condition);
		}
		$sql .= ' order by update_time desc';

		try {
			$result = $this->_connection->query($sql, $vars);
			if (count($result) > 0) 
				return $result;
			return false;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function getAllEmailOffersBySite($siteId) {
		$sql = 'select * from email_offers where site_id=? OR site_id=0 ';
		try {
			return $this->_connection->query($sql, array($siteId));
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function getAllEmailOffersBySiteAndTosentdate($siteId, $tosentDate) {
		$sql = 'SELECT * FROM email_offers WHERE site_id=? AND tosent_date=?';
		try {
			return $this->_connection->query($sql, array($siteId, $tosentDate));
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function addEmailOffer($name, $subject, $offerName, $offerId, $pictureUrl, $pictureWidth, $pictureHeight, $linkUrl, $banner_picture_url, $siteId, $order, $tosent_date) {
		$sql = 'insert into email_offers set name=?, subject=?, offer_name=?, offer_id=?, picture_url=?, picture_width=?, picture_height=?, link_url=?, banner_picture_url=?, site_id=?, ord=?, tosent_date=?, update_time=?';
		try {
			return $this->_connection->execute($sql, array($name, $subject, $offerName, $offerId, $pictureUrl, $pictureWidth, $pictureHeight, $linkUrl, $banner_picture_url, $siteId, $order, $tosent_date, time()));
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function updateEmailOffer($name, $subject, $offerName, $offerId, $pictureUrl, $pictureWidth, $pictureHeight, $linkUrl, $banner_picture_url, $siteId, $order, $tosent_date, $id) {
		$sql = 'update email_offers set name=?, subject=?, offer_name=?, offer_id=?, picture_url=?, picture_width=?, picture_height=?, link_url=?, banner_picture_url=?, site_id=?, ord=?, tosent_date=?, update_time=? where id=?';
		try {
			return $this->_connection->execute($sql, array($name, $subject, $offerName, $offerId, $pictureUrl, $pictureWidth, $pictureHeight, $linkUrl, $banner_picture_url, $siteId, $order, $tosent_date, time(), $id));
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function deleteEmailOffer($id) {
		$sql = 'delete from email_offers where id=?';
		try {
			return $this->_connection->execute($sql, array($id));
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}
	}
	
	public function getEmailOfferBannerBySiteId($siteId)
	{
		try {
			$sql = "SELECT v AS emailOfferId FROM configs WHERE k=? LIMIT 1 ";
			$result = $this->_connection->query($sql, array('siteId_' . $siteId));
			if(!empty($result)){
				$sql = " SELECT * FROM email_offers WHERE id=? ";
				$result2 = $this->_connection->query($sql, array($result[0]['emailOfferId']));
				if(!empty($result2) > 0){
					return $result2[0];
				}
			}
			return false;
		} catch (Exception $e) {
			Qlog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), Qlog::ERR);
			throw new Model_Exception($e->getMessage, $e->getCode());
		}		
	}
}